﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace ErrorCodeGenerator
{
	public partial class ErrorCodeGenerator : Form
	{
		public ErrorCodeGenerator()
		{
			InitializeComponent();
		}

		private void btnGenerate_Click(object sender, EventArgs e)
		{
			try
			{
				txtResultText.Text = string.Empty; // reset result field

				int startIndex = 0;

				if (!string.IsNullOrEmpty(txtStartIndex.Text)) // we check if the user entered anything in the textbox
				{
					startIndex = int.Parse(txtStartIndex.Text);
				}

				if (!string.IsNullOrEmpty(txtEvaluationText.Text))// we check if the user entered anything in the evaluation textbox
				{
					string filteredString = Regex.Replace(txtEvaluationText.Text, "[0-9]", "");

					string[] errorNameList = filteredString.Split(new string[] { "\n", "\r\n", ",", "=", " ", "\t\t" }, StringSplitOptions.RemoveEmptyEntries);

					if (errorNameList.Length > 0) // we have data to work with
					{
						foreach (string errorName in errorNameList)
						{
							txtResultText.Text += errorName + " = " + (startIndex++).ToString() + "," + Environment.NewLine;
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void txtStartIndex_TextChanged(object sender, EventArgs e)
		{
			if (cbxGenerateOnKeyPress.Checked)
			{
				btnGenerate_Click(sender, e);
			}
		}
	}
}